<?php
/**
 * 获取职位详情
 * User: yaosj
 * Date: 15-10-10
 * Time: 上午9:56
 */

class GetCareerDetail implements iController {
    
    public function output($queryArgs) {

        if (empty($queryArgs['uniq_code']))
            return genOutput(1000);

        global $cmsdb;

        if(!empty($queryArgs['province'])){

            //省级职位
            $jCode = intval(trim($queryArgs['uniq_code']));
            $sql = 'SELECT * FROM data_province_job_major WHERE id = :dCode';

            $result = $cmsdb->getRow($sql, array(':dCode' => $jCode));

            if (empty($result))
                return genOutput(1, array('count' => 0));

            $ret = array();
            $returnV = require __DIR__ . '/code2name.config.php';
            $proId = $queryArgs['province'];
            //针对每个省，返回不一样
            $sql = 'SELECT * FROM relation_job_major WHERE province_id = :pCode AND detail_order > 0 ORDER BY detail_order';
            $rjm = $cmsdb->getResults($sql, array(':pCode' => $proId));

            if (empty($rjm)) {
                //TODO 为空的时候怎么处理
                $returnV = require __DIR__ . '/code2name.config.php';
            }

            foreach ($rjm as $item) {

                if (is_object($item))
                    $item = (array)$item;
                foreach ($result as $code => $value) {
                    //返回值格式转化
                    if ($item['table_column'] == $code) {
                        if ($code == 'is_3_1' || $code == 'is_west_volunteer' ||
                            $code == 'is_college_officer' || $code == 'is_special_teacher' ||
                            $code == 'is_one_of_four')
                            $value = $value == 1 ? '是' : '否';
                        if ($code == 'political_status' || $code == 'min_junior_year')
                            $value = empty($value) ? '不限' : $value;
                        $ret[] = array('key' => $item['shown_name'], 'value' => $value);
                    };
                }
            }
            if (!empty($queryArgs['user_id'])) {

                $userId = intval($queryArgs['user_id']);
                global $uddb;
                $sql = 'SELECT * FROM user_job_collection WHERE user_id=:user_id AND department_job_code =:code AND status = :status';
                $a = $uddb->getRow($sql, array(':user_id' => $userId, ':code' => $jCode, ':status' => 1));
                if (empty($a))
                    $is_collected = 0;
                else
                    $is_collected = 1;

            } else {
                $is_collected = 0;
            }
            return genOutput(1, array('is_collected' => $is_collected, 'data' => $ret));

        } else {
            //国家职位
            $jCode = trim($queryArgs['uniq_code']);
            $sql = 'SELECT * FROM data_job_major WHERE id = :dCode';

            $result = $cmsdb->getRow($sql, array(':dCode' => $jCode));

            if (empty($result))
                return genOutput(1, array('count' => 0));

            $ret = array();
            //原来的department_job_code在加入省职位后不唯一了，因此使用id作为唯一标示。
            $returnV = require __DIR__ . '/code2name.config.php';

            $returnV = $returnV['all'];
            foreach ($result as $code => $value) {
                //将0，1转化成是否
                if ($code == 'is_3_1' || $code == 'is_west_volunteer' || $code == 'is_college_officer' || $code == 'is_special_teacher')
                    $value = $value == 1 ? '是' : '否';
                if (array_key_exists($code, $returnV))
                    $ret[$code] = $value;
            }
            //将电话号码合并成数组
            if (!empty($result->consult_telephone_1))
                $ret['consult_telephone'][] = $result->consult_telephone_1;
            if (!empty($result->consult_telephone_2))
                $ret['consult_telephone'][] = $result->consult_telephone_2;
            if (!empty($result->consult_telephone_3))
                $ret['consult_telephone'][] = $result->consult_telephone_3;

            //

            if (!empty($queryArgs['user_id'])) {

                $userId = intval($queryArgs['user_id']);
                global $uddb;
                $sql = 'SELECT * FROM user_job_collection WHERE user_id=:user_id AND department_job_code =:code AND status = :status';
                $a = $uddb->getRow($sql, array(':user_id' => $userId, ':code' => $jCode, ':status' => 1));
                if (empty($a))
                    $is_collected = 0;
                else
                    $is_collected = 1;

            } else {
                $is_collected = 0;
            }
            return genOutput(1, array('is_collected' => $is_collected, 'data' => $ret));
        }
    }
}